package com.icss.mqsp.data.process.impl;

import com.icss.mqsp.data.process.IDataProcessor;
import com.icss.mqsp.model.ReportDefine;
import com.icss.pangu.logging.Log;
import com.icss.pangu.logging.LogFactory;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class ChartDataProcessor
  implements IDataProcessor
{
  private Log log = LogFactory.getLog(ChartDataProcessor.class);
  
  public JSONObject processData(ReportDefine rd, String data, Map<String, String> condition)
  {
    JSONArray jsdata = JSONArray.fromObject(data);
    int dataSize = jsdata.size();
    
    String subType = rd.getSubType();
    
    JSONObject js = new JSONObject();
    js.put("appid", rd.getAppid());
    js.put("title", rd.getTitle());
    this.log.info(subType);
    JSONArray chartArray = new JSONArray();
    this.log.info("jsdata:" + jsdata.toString());
    if ("pie".equals(subType))
    {
      JSONArray seriesArray = new JSONArray();
      for (int i = 0; i < dataSize; i++)
      {
        JSONObject pieJson = jsdata.getJSONObject(i);
        JSONObject newjs = new JSONObject();
        newjs.put("name", pieJson.get("col1"));
        newjs.put("value", pieJson.get("col2"));
        seriesArray.add(i, newjs);
      }
      js.put("seriesdata", seriesArray);
    }
    else if ("line".equals(subType))
    {
      StringBuffer xAxisData = new StringBuffer();
      xAxisData.append("[");
      for (int i = 0; i < dataSize; i++)
      {
        JSONObject lineJson = jsdata.getJSONObject(i);
        if (i == dataSize - 1) {
          xAxisData.append("'" + lineJson.get("col1") + "'");
        } else {
          xAxisData.append("'" + lineJson.get("col1") + "',");
        }
      }
      xAxisData.append("]");
      js.put("xAxisData", xAxisData.toString());
      
      JSONArray charts = rd.getCharts();
      for (int j = 0; j < charts.size(); j++)
      {
        StringBuffer value = new StringBuffer();
        value.append("[");
        
        JSONObject jschart = charts.getJSONObject(j);
        JSONObject chartJson = new JSONObject();
        chartJson.put("name", jschart.getString("name"));
        chartJson.put("type", jschart.getString("type"));
        if (j == 0) {
          chartJson.put("label", "{normal:{show: true,position: 'top'}}");
        } else if ("bar".equals(jschart.getString("type"))) {
          chartJson.put("label", "{normal:{show: true,position: 'top'}}");
        }
        for (int i = 0; i < dataSize; i++)
        {
          JSONObject lineJson = jsdata.getJSONObject(i);
          if (i == dataSize - 1) {
            value.append(lineJson.get(jschart.get("col")));
          } else {
            value.append(lineJson.get(jschart.get("col")) + ",");
          }
        }
        value.append("]");
        chartJson.put("data", value.toString());
        chartArray.add(j, chartJson);
      }
      js.put("seriesdata", chartArray);
    }
    else if ("bar-line".equals(subType))
    {
      StringBuffer xAxisData = new StringBuffer();
      xAxisData.append("[");
      for (int i = 0; i < dataSize; i++)
      {
        JSONObject lineJson = jsdata.getJSONObject(i);
        if (i == dataSize - 1) {
          xAxisData.append("'" + lineJson.get("col1") + "'");
        } else {
          xAxisData.append("'" + lineJson.get("col1") + "',");
        }
      }
      xAxisData.append("]");
      js.put("xAxisData", xAxisData.toString());
      
      JSONArray charts = rd.getCharts();
      for (int j = 0; j < charts.size(); j++)
      {
        StringBuffer value = new StringBuffer();
        value.append("[");
        
        JSONObject jschart = charts.getJSONObject(j);
        JSONObject chartJson = new JSONObject();
        chartJson.put("name", jschart.getString("name"));
        chartJson.put("type", jschart.getString("type"));
        chartJson.put("label", "{normal:{show: true,position: 'top'}}");
        if ("line".equals(jschart.getString("type"))) {
          chartJson.put("yAxisIndex", Integer.valueOf(1));
        }
        for (int i = 0; i < dataSize; i++)
        {
          JSONObject lineJson = jsdata.getJSONObject(i);
          if (i == dataSize - 1) {
            value.append(lineJson.get(jschart.get("col")));
          } else {
            value.append(lineJson.get(jschart.get("col")) + ",");
          }
        }
        value.append("]");
        chartJson.put("data", value.toString());
        chartArray.add(j, chartJson);
      }
      js.put("seriesdata", chartArray);
    }
    else if ("line-bar".equals(subType))
    {
      StringBuffer legendData = new StringBuffer();
      JSONArray charts = rd.getCharts();
      this.log.info("charts:" + rd.getCharts());
      legendData.append("[");
      for (int j = 0; j < charts.size(); j++)
      {
        JSONObject jschart = charts.getJSONObject(j);
        String legend = "";
        try
        {
          legend = jschart.getString("legend").toString() == null ? "" : jschart.getString("legend").toString();
        }
        catch (JSONException e)
        {
          this.log.info("该属性不存在！");
        }
        legendData.append("'" + legend + "',");
      }
      legendData.append("]");
      this.log.info(legendData);
      js.put("legendData", legendData.toString());
      
      StringBuffer xAxisData = new StringBuffer();
      xAxisData.append("[");
      for (int i = 0; i < dataSize; i++)
      {
        JSONObject lineJson = jsdata.getJSONObject(i);
        if (i == dataSize - 1) {
          xAxisData.append("'" + lineJson.get("col1") + "'");
        } else {
          xAxisData.append("'" + lineJson.get("col1") + "',");
        }
      }
      xAxisData.append("]");
      js.put("xAxisData", xAxisData.toString());
      
      this.log.info("charts:" + charts.toString());
      JSONArray yAxisArray = new JSONArray();
      
      JSONObject yAxisJson = new JSONObject();
      for (int i = 0; i < charts.size(); i++)
      {
        String beforeType = "";
        if (i > 0) {
          beforeType = charts.getJSONObject(i - 1).getString("type").toString();
        }
        String nowType = charts.getJSONObject(i).getString("type").toString();
        String unit = "";
        try
        {
          unit = charts.getJSONObject(i).getString("unit").toString() == null ? "" : charts.getJSONObject(i).getString("unit").toString();
        }
        catch (JSONException e)
        {
          this.log.info("该属性不存在！");
        }
        int y = 0;
        if (i == 0)
        {
          yAxisJson.put("name", unit);
          yAxisJson.put("type", "value");
        }
        else
        {
          if (beforeType.equals(nowType)) {
            continue;
          }
          y = 1;
          yAxisJson.put("name", unit);
          yAxisJson.put("type", "value");
        }
        yAxisArray.add(y, yAxisJson);
      }
      js.put("yAxisData", yAxisArray);
      for (int j = 0; j < charts.size(); j++)
      {
        StringBuffer value = new StringBuffer();
        value.append("[");
        
        JSONObject jschart = charts.getJSONObject(j);
        String markpoint = "";
        try
        {
          markpoint = jschart.getString("markPoint").toString();
        }
        catch (Exception localException) {}
        String label = "";
        try
        {
          label = jschart.getString("label").toString();
        }
        catch (Exception localException1) {}
        JSONObject chartJson = new JSONObject();
        chartJson.put("name", jschart.getString("name"));
        chartJson.put("type", jschart.getString("type"));
        if ("bar".equals(jschart.getString("type").toString())) {
          chartJson.put("barMaxWidth", Integer.valueOf(Integer.parseInt(jschart.getString("barMaxWidth"))));
        }
        if (("line".equals(jschart.getString("type").toString())) && ("1".equals(markpoint))) {
          chartJson.put("markPoint", "{data:[{type:'max',name:'最大值',itemStyle : {normal : {color:'#66CC99',symbolSize : 5 ,label:{show: true,position:'top'}}}},                          {type:'min',name:'最小值',itemStyle : {normal : {color:'#66CC99',symbolSize : 5 ,label:{show: true,position:'top'}}}}]}");
        }
        if ("1".equals(label)) {
          chartJson.put("itemStyle", "{ normal: {label : {show: true, position: 'top',textStyle : {color : '#000'}}}}");
        }
        for (int i = 0; i < dataSize; i++)
        {
          JSONObject lineJson = jsdata.getJSONObject(i);
          if (i == dataSize - 1) {
            value.append(lineJson.get(jschart.get("col")));
          } else {
            value.append(lineJson.get(jschart.get("col")) + ",");
          }
        }
        value.append("]");
        chartJson.put("data", value.toString());
        chartArray.add(j, chartJson);
      }
      js.put("seriesdata", chartArray);
    }
    else if ("single_bar".equals(subType))
    {
      StringBuffer legendData = new StringBuffer();
      JSONArray charts = rd.getCharts();
      this.log.info("charts:" + rd.getCharts());
      legendData.append("[");
      for (int j = 0; j < charts.size(); j++)
      {
        JSONObject jschart = charts.getJSONObject(j);
        String legend = "";
        try
        {
          legend = jschart.getString("legend").toString() == null ? "" : jschart.getString("legend").toString();
        }
        catch (JSONException e)
        {
          this.log.info("该属性不存在！");
        }
        legendData.append("'" + legend + "',");
      }
      legendData.append("]");
      js.put("legendData", legendData.toString());
      
      StringBuffer xAxisData = new StringBuffer();
      xAxisData.append("[");
      for (int i = 0; i < dataSize; i++)
      {
        JSONObject lineJson = jsdata.getJSONObject(i);
        if (i == dataSize - 1) {
          xAxisData.append("'" + lineJson.get("col1") + "'");
        } else {
          xAxisData.append("'" + lineJson.get("col1") + "',");
        }
      }
      xAxisData.append("]");
      js.put("xAxisData", xAxisData.toString());
      
      this.log.info("charts:" + charts.toString());
      JSONArray yAxisArray = new JSONArray();
      
      JSONObject yAxisJson = new JSONObject();
      for (int i = 0; i < charts.size(); i++)
      {
        String beforeType = "";
        if (i > 0) {
          beforeType = charts.getJSONObject(i - 1).getString("type").toString();
        }
        String nowType = charts.getJSONObject(i).getString("type").toString();
        String unit = "";
        try
        {
          unit = charts.getJSONObject(i).getString("unit").toString() == null ? "" : charts.getJSONObject(i).getString("unit").toString();
        }
        catch (JSONException e)
        {
          this.log.info("该属性不存在！");
        }
        int y = 0;
        if (i == 0)
        {
          yAxisJson.put("name", unit);
          yAxisJson.put("type", "value");
        }
        else
        {
          if (beforeType.equals(nowType)) {
            continue;
          }
          this.log.info(beforeType.equals(nowType) + "===" + i);
          y = 1;
          yAxisJson.put("name", unit);
          yAxisJson.put("type", "value");
        }
        yAxisArray.add(y, yAxisJson);
      }
      js.put("yAxisData", yAxisArray);
      for (int j = 0; j < charts.size(); j++)
      {
        JSONObject jschart = charts.getJSONObject(j);
        JSONObject bqDataJson = new JSONObject();
        JSONObject tqDataJson = new JSONObject();
        for (int i = 0; i < 2; i++)
        {
          JSONObject bqJson = jsdata.getJSONObject(0);
          JSONObject tqJson = jsdata.getJSONObject(1);
          if (i == 0)
          {
            bqDataJson.put("name", jschart.getString("name"));
            bqDataJson.put("type", jschart.getString("type"));
            bqDataJson.put("stack", jschart.getString("stack"));
            bqDataJson.put("label", "{ normal: {show: true}}");
            bqDataJson.put("barMaxWidth", "50%");
            bqDataJson.put("itemStyle", "{ normal: {label : {show: true, position: 'top'}}}");
            bqDataJson.put("data", "[" + bqJson.get(jschart.get("col")) + ",'-']");
            chartArray.add(bqDataJson);
          }
          else
          {
            tqDataJson.put("name", jschart.getString("name"));
            tqDataJson.put("type", jschart.getString("type"));
            tqDataJson.put("stack", jschart.getString("stack"));
            bqDataJson.put("label", "{ normal: {show: true}");
            tqDataJson.put("barMaxWidth", "50%");
            tqDataJson.put("itemStyle", "{ normal: {label : {show: true, position: 'top'}}}");
            tqDataJson.put("data", "['-'," + tqJson.get(jschart.get("col")) + "]");
            chartArray.add(tqDataJson);
          }
        }
      }
      js.put("seriesdata", chartArray);
    }
    else if ("vertcal".equals(subType))
    {
      StringBuffer xAxisData = new StringBuffer();
      xAxisData.append("[");
      for (int i = 0; i < dataSize; i++)
      {
        JSONObject xAxisJson = jsdata.getJSONObject(i);
        if (i == dataSize - 1) {
          xAxisData.append("'" + xAxisJson.get("col1") + "'");
        } else {
          xAxisData.append("'" + xAxisJson.get("col1") + "',");
        }
      }
      xAxisData.append("]");
      js.put("xAxisData", xAxisData.toString());
      js.put("axisLabel", "{ interval:0}");
      
      StringBuffer legendData = new StringBuffer();
      JSONArray charts = rd.getCharts();
      this.log.info("charts:" + rd.getCharts());
      legendData.append("[");
      for (int j = 0; j < charts.size(); j++)
      {
        JSONObject jschart = charts.getJSONObject(j);
        legendData.append("'" + jschart.getString("legend") + "',");
      }
      legendData.append("]");
      js.put("legendData", legendData.toString());
      

      JSONArray yAxisArray = new JSONArray();
      JSONObject yAxisJson = new JSONObject();
      for (int i = 0; i < 1; i++)
      {
        String unit = "";
        try
        {
          unit = charts.getJSONObject(i).getString("unit").toString() == null ? "" : charts.getJSONObject(i).getString("unit").toString();
        }
        catch (JSONException e)
        {
          this.log.info("该属性不存在！");
        }
        yAxisJson.put("name", "销量（箱）");
        yAxisJson.put("axisLine", "{show:false}");
        yAxisJson.put("splitArea", "{show:false}");
        yAxisJson.put("splitLine", "{show:false}");
        yAxisArray.add(0, yAxisJson);
      }
      js.put("yAxisData", yAxisArray);
      for (int j = 0; j < charts.size(); j++)
      {
        StringBuffer value = new StringBuffer();
        value.append("[");
        
        JSONObject jschart = charts.getJSONObject(j);
        JSONObject chartJson = new JSONObject();
        chartJson.put("name", jschart.getString("name"));
        chartJson.put("type", jschart.getString("type"));
        chartJson.put("stack", jschart.getString("stack"));
        chartJson.put("barMaxWidth", Integer.valueOf(Integer.parseInt(jschart.getString("barMaxWidth"))));
        for (int i = 0; i < dataSize; i++)
        {
          JSONObject lineJson = jsdata.getJSONObject(i);
          if (i == dataSize - 1) {
            value.append(lineJson.get(jschart.get("col")));
          } else {
            value.append(lineJson.get(jschart.get("col")) + ",");
          }
        }
        value.append("]");
        chartJson.put("data", value.toString());
        chartArray.add(j, chartJson);
      }
      js.put("seriesdata", chartArray);
    }
    else if ("layer-bar".equals(subType))
    {
      StringBuffer xAxisData = new StringBuffer();
      xAxisData.append("[");
      for (int i = 0; i < dataSize; i++)
      {
        JSONObject xAxisJson = jsdata.getJSONObject(i);
        if (i == dataSize - 1) {
          xAxisData.append("'" + xAxisJson.get("col1") + "'");
        } else {
          xAxisData.append("'" + xAxisJson.get("col1") + "',");
        }
      }
      xAxisData.append("]");
      js.put("xAxisData", xAxisData.toString());
      

      js.put("axisLabel", "{ interval:0,rotate:30}");
      
      StringBuffer legendData = new StringBuffer();
      JSONArray charts = rd.getCharts();
      this.log.info("charts:" + rd.getCharts());
      legendData.append("[");
      for (int j = 0; j < charts.size(); j++)
      {
        JSONObject jschart = charts.getJSONObject(j);
        legendData.append("'" + jschart.getString("name") + "',");
      }
      legendData.append("]");
      js.put("legendData", legendData.toString());
      for (int j = 0; j < charts.size(); j++)
      {
        StringBuffer value = new StringBuffer();
        value.append("[");
        JSONObject jschart = charts.getJSONObject(j);
        JSONObject chartJson = new JSONObject();
        chartJson.put("name", jschart.getString("name"));
        chartJson.put("type", jschart.getString("type"));
        chartJson.put("stack", jschart.getString("stack"));
        chartJson.put("barCategoryGap", "50%");
        if (1 == Integer.parseInt(jschart.getString("itemStyle"))) {
          chartJson.put("itemStyle", "{normal: {color: '#2ec7c9',barBorderColor: '#2ec7c9',barBorderWidth: 2,barBorderRadius:0}}");
        } else {
          chartJson.put("itemStyle", "{normal: {color: '#ffffcc',barBorderColor: '#2ec7c9',barBorderWidth: 2,barBorderRadius:0}}");
        }
        for (int i = 0; i < dataSize; i++)
        {
          JSONObject lineJson = jsdata.getJSONObject(i);
          if (i == dataSize - 1) {
            value.append(lineJson.get(jschart.get("col")));
          } else {
            value.append(lineJson.get(jschart.get("col")) + ",");
          }
        }
        value.append("]");
        chartJson.put("data", value.toString());
        chartArray.add(j, chartJson);
      }
      js.put("seriesdata", chartArray);
      
      StringBuffer unitData = new StringBuffer();
      unitData.append("[");
      String unit = "";
      try
      {
        unit = charts.getJSONObject(0).getString("unit").toString() == null ? "" : charts.getJSONObject(0).getString("unit").toString();
      }
      catch (JSONException e)
      {
        this.log.info("该属性不存在！");
      }
      unitData.append("'" + unit + "'");
      unitData.append("]");
      js.put("unitData", unitData.toString());
    }
    else if ("stack_bar".equals(subType))
    {
      StringBuffer xAxisData = new StringBuffer();
      xAxisData.append("[");
      for (int i = 0; i < dataSize; i++)
      {
        JSONObject lineJson = jsdata.getJSONObject(i);
        if (i == dataSize - 1) {
          xAxisData.append("'" + lineJson.get("col1") + "'");
        } else {
          xAxisData.append("'" + lineJson.get("col1") + "',");
        }
      }
      xAxisData.append("]");
      js.put("xAxisData", xAxisData.toString());
      
      JSONArray charts = rd.getCharts();
      for (int j = 0; j < charts.size(); j++)
      {
        StringBuffer value = new StringBuffer();
        value.append("[");
        
        JSONObject jschart = charts.getJSONObject(j);
        JSONObject chartJson = new JSONObject();
        chartJson.put("name", jschart.getString("name"));
        chartJson.put("stack", "堆栈图");
        chartJson.put("type", jschart.getString("type"));
        if ("true".equals(jschart.getString("show").toString())) {
          chartJson.put("itemStyle", "normal: {label : { show: true,  position: 'top', formatter: function (params) { for (var i = 0, l = option.xAxis[0].data.length; i < l; i++) {  if (option.xAxis[0].data[i] == params.name) {  return parseFloat((option.series[0].data[i]/(option.series[0].data[i] + params.value)*100).toFixed(2))+'%';}}}}}");
        }
        for (int i = 0; i < dataSize; i++)
        {
          JSONObject lineJson = jsdata.getJSONObject(i);
          if (i == dataSize - 1) {
            value.append(lineJson.get(jschart.get("col")));
          } else {
            value.append(lineJson.get(jschart.get("col")) + ",");
          }
        }
        value.append("]");
        chartJson.put("data", value.toString());
        chartArray.add(j, chartJson);
      }
      js.put("seriesdata", chartArray);
    }
    return js;
  }
}
